package com.ysalliance.getFan.organization.ui.h5;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.google.gson.JsonObject;
import com.just.agentweb.AgentWeb;
import com.ysalliance.getFan.organization.Bean.TaskBean;
import com.ysalliance.getFan.organization.Bean.UserBean;
import com.ysalliance.getFan.organization.LucasUtil;
import com.ysalliance.getFan.organization.myview.dialog.JinOkDialog;

import org.litepal.LitePal;

import static com.ysalliance.getFan.organization.FanerConstant.TASKDETAIL;

/**
 * Created by cenxiaozhong on 2017/5/14.
 *  source code  https://github.com/Justson/AgentWeb
 */

public class AndroidInterface {

    private Handler deliver = new Handler(Looper.getMainLooper());
    private AgentWeb agent;
    private Context context;



    public AndroidInterface(AgentWeb agent, Context context) {
        this.agent = agent;
        this.context = context;
    }



    private TaskBean taskBean;  //针对业务传参的
    public AndroidInterface(AgentWeb agent, Context context, TaskBean taskBean) {
        this.agent = agent;
        this.context = context;
        this.taskBean = taskBean;
    }


    @JavascriptInterface
    public void callAndroidExample(final String msg) {


        deliver.post(new Runnable() {
            @Override
            public void run() {

                Log.i("Info", "main Thread:" + Thread.currentThread());
                Toast.makeText(context.getApplicationContext(), "" + msg, Toast.LENGTH_LONG).show();
            }
        });


        Log.i("Info", "Thread:" + Thread.currentThread());

    }




//    @JavascriptInterface
//    public void callAndroid(final String msg) {
//        deliver.post(new Runnable() {
//            @Override
//            public void run() {
////                Log.i("Info", "main Thread:" + Thread.currentThread());
////                Toast.makeText(context.getApplicationContext(), "" + msg, Toast.LENGTH_LONG).show();
//                Intent intent = new Intent(Utils.getApp(),  ChoujiangActivity.class);
//                intent.putExtra("url" ,msg);
//                ActivityUtils.startActivity(intent);
//            }
//        });
////        Log.i("Info", "Thread:" + Thread.currentThread());
//    }






    @JavascriptInterface
    public void showMsg(final String msg) {
        deliver.post(new Runnable() {
            @Override
            public void run() {
                JinOkDialog dialog = new JinOkDialog(context, msg,"确定");
                dialog.show();
            }
        });
    }





//    //发现页活动中点击某个活动的事件
//    @JavascriptInterface
//    public void  activity2detail(final String msg) {
//
//        deliver.post(new Runnable() {
//            @Override
//            public void run() {
//                Intent intent = new Intent(Utils.getApp(), HuodongSituationActivity.class);
//                intent.putExtra("url" , msg);
//                ActivityUtils.startActivity(intent);
//            }
//        });
//    }


    //发现页图片的点击事件
    @JavascriptInterface
    public void  showImage( String recieve_str) {
        LogUtils.e("opop  jiekou", recieve_str);
    }










    @JavascriptInterface
    public void fetchDeviceInfo() {
        LogUtils.e("反馈的设备信息", DeviceUtils.getUniqueDeviceId(), DeviceUtils.getModel());
        agent.getJsAccessEntrace().quickCallJs("fetchInfo", DeviceUtils.getUniqueDeviceId(), DeviceUtils.getModel());
    }





    @JavascriptInterface
    public void  getSign() {


        String timestamp_str = (TimeUtils.getNowMills()+"").substring(0,10);
        if(!LucasUtil.isLogin()){
            return;
        }
        UserBean user = LitePal.findFirst(UserBean.class);

        String sign = EncryptUtils.encryptSHA1ToString(    user.getUser_id() + '|' + user.getFirst_name() + '*' + "institution" + '%' + timestamp_str  ).toLowerCase() +"a" ;

        JsonObject jsonObject  = new JsonObject();
        jsonObject.addProperty("sign",sign);
        jsonObject.addProperty("version", AppUtils.getAppVersionName());
        jsonObject.addProperty("mobile",user.getUser_id());
        jsonObject.addProperty("timestamp", timestamp_str);


        agent.getJsAccessEntrace().quickCallJs("dynamic" ,
                jsonObject.toString());

    }



    //给任务详情页传参的
    @JavascriptInterface
    public void  taskinfo() {
        if(taskBean!=null){
            agent.getJsAccessEntrace().quickCallJs("taskinfo", taskBean.getName(), taskBean.getCreate_time());
        }
    }


    //给任务详情页点击具体学院的学习情况  进行跳转
    @JavascriptInterface
    public void mission2review(final String url) {
        Intent intent = new Intent(Utils.getApp(), TaskStuDetailH5Activity.class);
        intent.putExtra("url", url);
        ActivityUtils.startActivity(intent);
    }






}
